/**
 * 
 */
package com.colombounplug.geoshopper;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

/**
 * Itemized Overlay that designs the custom dialogs.
 * @author anuradha.uduwage
 *
 */
public class StoreMarkerLayer extends ItemizedOverlay {

	private ArrayList<OverlayItem> mStoreOverlays = new ArrayList<OverlayItem>();
	Context mContext;
	
	/**
	 * @param defaultMarker
	 */
	public StoreMarkerLayer(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		populate();
	}
	
	/**
	 * Second constructor to handle touch events.
	 * @param defaultMarker
	 * @param context
	 */
	public StoreMarkerLayer(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}
	
	/**
	 * Adds an OverlayItem to the {@link ArrayList} this actually calls the populate() 
	 * and prepare it for drawing. --as of my understanding
	 * @param overlay
	 */
	public void addOverlayItem(OverlayItem overlay) {
		mStoreOverlays.add(overlay);
		populate();
	}

	/**
	 * Called by populate method to retrieve the given OverlayItem
	 */
	@Override
	protected OverlayItem createItem(int i) {
		return mStoreOverlays.get(i);
	}

	/**
	 * Return the number of overlay items in the {@link ArrayList}
	 */
	@Override
	public int size() {
		return mStoreOverlays.size();
	}

	//before changes
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mStoreOverlays.get(index);
		String information = item.getSnippet();
		final String[] stores = information.split(";");
		for(int i =0; i < stores.length; i++) {
			Log.v("Dialog:" , "=" + stores[i]);
		}
		if(mContext != null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(R.layout.custom_dialog, null);
			LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout_root);
			
			for(int i =0; i < stores.length; i++) {
				TextView tv = new TextView(mContext);
				int j = i + 1;
				tv.setText("*"+stores[i]);
				tv.setTextSize(20);
				layout.addView(tv);
			}
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setNegativeButton("Ok", null);
			dialog.setView(view);
			dialog.show();
		}
		return true;
	}
}
